import { EDataKey } from "../enum";
import { Util } from "../uitls/Util";
import { allItems } from "./goods/allGoods";
import { BagItem } from "./goods/goodsInfo"







export class PlayerData {
    /** 登录时间 */
    loginTime: number = 0;
    /** 金币 */
    gold: number = 0;

    /** 玩家属性 */
    ap: number = 5;
    dp: number = 2;
    hp: number = 100;
    exp: number = 0;

    backpack: BagItem[] = [];



    getPackage() {
        let data = {};
        data[EDataKey.LOGIN_TIME] = this.loginTime;
        data[EDataKey.GOLD] = this.gold;

        data[EDataKey.AP] = this.ap;
        data[EDataKey.DP] = this.dp;
        data[EDataKey.HP] = this.hp;
        data[EDataKey.EXP] = this.exp;

        data[EDataKey.BACKPACK] = this.backpack;
        





        return data;
    }

    setPackage(data: {}) {
        if (!data) return;
        

        this.loginTime = Util.getSafeData<number>(data[EDataKey.LOGIN_TIME], 0);
        this.gold = Util.getSafeData<number>(data[EDataKey.GOLD], 0);


        this.ap = Util.getSafeData<number>(data[EDataKey.AP], 0);
        this.dp = Util.getSafeData<number>(data[EDataKey.DP], 0);
        this.hp = Util.getSafeData<number>(data[EDataKey.HP], 0);
        this.exp = Util.getSafeData<number>(data[EDataKey.EXP], 0);

        this.backpack = Util.getSafeData<BagItem[]>(data[EDataKey.BACKPACK], []);

        
        





    }


    /**
     * 根据物品id获取物品对象
     * @param id 物品id
     * @returns 物品对象，如果不存在则返回null
     */
    // getItemById(id: number): BagItem | null {
    //     // 先从背包中查找
    //     const backpackItem = this.backpack.find(item => item.id === id);
    //     if (backpackItem) {
    //         return backpackItem;
    //     }

    //     // 如果背包中没有，从全局物品库查找（用于预览或默认物品）
    //     const globalItem = allItems.find(item => item.id === id);
    //     if (globalItem) {
    //         // 返回深拷贝，避免修改原始数据
    //         return JSON.parse(JSON.stringify(globalItem));
    //     }

    //     return null;
    // }










}